package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.admin.Admin;
import com.cskaoyan.service.admin.AdminService;
import com.cskaoyan.utils.ReflectionSetJavaBeanUtils;
import com.cskaoyan.utils.ValidationUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-18
 */
@RequestMapping("admin/admin")
@RestController
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequiresPermissions("admin:admin:list")
    @RequestMapping("list")
    // http://localhost:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc
    public BaseRespVo list(Admin admin) {
        PageDataVo pageData = adminService.query(admin);
        return BaseRespVo.ok(pageData);
    }

    @RequiresPermissions("admin:admin:create")
    @PostMapping("create")
    // http://localhost:8083/admin/admin/create
    public BaseRespVo create(@RequestBody @Validated Admin admin, BindingResult bindingResult) {
        // 工具类，传入对应实例通过反射设置值
        ReflectionSetJavaBeanUtils.setJavaBean(admin);
        if (!adminService.create(admin))
            return BaseRespVo.error("添加失败");
        return BaseRespVo.ok(admin);
    }

    @RequiresPermissions("admin:admin:update")
    @PostMapping("update")
    // http://localhost:8083/admin/admin/update
    public BaseRespVo update(@RequestBody @Validated Admin admin, BindingResult bindingResult) {
        if (!adminService.update(admin))
            return BaseRespVo.error("修改失败");
        return BaseRespVo.ok(admin);
    }

    @RequiresPermissions("admin:admin:delete")
    @PostMapping("delete")
    // http://localhost:8083/admin/admin/delete
    public BaseRespVo delete(@RequestBody @Validated Admin admin, BindingResult bindingResult) {
        if (!adminService.delete(admin))
            return BaseRespVo.error("删除失败");
        return BaseRespVo.ok();
    }
}
